/**
 * Created by Administrator on 2017/4/6.
 */
var React = require('react');

var tiyan = require('../main/tiyan');
var ajax = require('../tools/ajax');
var Icon = require('../components/Icon');
var MsgBox = require('../components/MsgBox');

module.exports = React.createClass({
    getInitialState: function (){
        return {
            tiyanInfo:[],//评论单条信息
            userInfo:[],//用户信息
            zan_number:0,
            zanFlag:false,
        }
    },

    componentDidMount: function () {
        this.getUserInfoByUid();
    },
    //用户信息
    getUserInfoByUid(){
        var p = this;
        var userId = this.props.tiyanInfo.user_id;
        ajax("POST","getUserInfoByUid",{userId:userId},function(dt){
            if(dt&&dt.success){
                p.setState({userInfo:dt.data[0]});
            }
        })
    },
    /**点赞*/
    UpdateZan(){
        var p=this;
        var tiyanInfo = this.props.tiyanInfo;
        var user_id = localStorage.getItem("userId");
        var comment_id = tiyanInfo.Id;
        ajax("POST","UpdateZan",{
            user_id:user_id,
            comment_id:comment_id
        },function(dt){
            console.log(dt);
            if(dt&&dt.success){
                if(dt.data[0]){
                    p.refs.MsgBox.setState({
                        type: "no",
                        timeout:1000,
                        show:true,
                        word:"该评论已点赞"
                    });
                }else{
                    p.refs.MsgBox.setState({
                        type: "no",
                        timeout:1000,
                        show:true,
                        word:"点赞成功"
                    });
                }
                p.props.zan();
                p.setState({zan_number:tiyanInfo.zan_number});
            }
        });
    },
    /**评论*/
    pinglun(e){
        var tiyanId = e.currentTarget.getAttribute("data");
        localStorage.setItem("tiyanId",tiyanId);
        location.href = "#tiyaninfo";
    },
    render: function () {
        var p = this;
        var obj = this.props.tiyanInfo;
        var user = this.state.userInfo;
        var AllPic = JSON.parse(obj.photo);
        if(AllPic){
            var picList = AllPic.map(function (obj) {
                return(
                    <img key={obj}  className="pinl_img" src={obj}/>
                )
            });
        }
        return (
            <div className="pinl-p bottom">
                <div className="pinl_logo">
                    <img src={user.photo?user.photo:"http://115.29.198.39:8080/upload/app/857749830834048835.png"} />
                </div>
                <div className="pinl_con">
                    <p className="pinl_name">{user.nickname?user.nickname:user.account}</p>
                    <p className="pinl_des">{obj.content}</p>
                    <div className="pinl_shaitu">
                        {picList}
                    </div>
                    <div className="pinl_c">
                        <div className="tiyanBtn" onClick={p.UpdateZan}>
                            <Icon className="zan" icon="#icon-dianzan" />
                            <span>{obj.zan_number}</span>
                        </div>
                        <div className="tiyanBtn" onClick={p.pinglun} data={obj.Id}>
                            <Icon  className="liaotian" icon="#icon-groupcopy5"/>
                            <span>{obj.comment_number}</span>
                        </div>
                        <div className="tiyanBtn">
                            <Icon className="share" icon="#icon-fenxiang"/>
                            <span>{obj.share_number}</span>
                        </div>
                    </div>
                </div>
                <MsgBox ref="MsgBox"></MsgBox>
            </div>
        )
    }
});